package com.itheima.d2_xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.transaction.xa.XAResource;
import java.util.List;

/*
     目标：掌握使用Dom4j框架解析XML文件。
 */
public class Dom4JTest1 {
    public static void main(String[] args) throws SAXException, DocumentException {
        // 1. 读取XML文件
        final SAXReader saxReader = new SAXReader();

        // 2. 获取document对象
        final Document document = saxReader.read("properties-xml-logback-app/src/config/book.xml");

        // 3. 获取根节点
        final Element rootElement = document.getRootElement();

        // 4. 获取所有字节点
        final List<Element> elements = rootElement.elements("book");

        // 5. 循环读取每一个book数据
        for (Element element: elements) {
            final String name = element.elementText("name");
            final String auther = element.elementText("auther");
            final String price = element.elementText("price");
            System.out.println(name + " --> " + auther + " --> " + price);

        }
    }
}
